package com.example.boxugu.myapplication.activity;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.boxugu.myapplication.Fragment.CourseFragment;
import com.example.boxugu.myapplication.Fragment.PracticeFragment;
import com.example.boxugu.myapplication.Moder.PracticeModer;
import com.example.boxugu.myapplication.Moder.PractisedetailModer;
import com.example.boxugu.myapplication.R;
import com.google.gson.Gson;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;

public class Practisedetai_lActivity extends AppCompatActivity {
String id;
ImageButton mBackbtn;
RecyclerView recyclerView;
TextView title;
MyAdapter myAdapter;
ArrayList<PractisedetailModer.SubjectList>subjectList=new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_practisedetai_l);
        if (getIntent()!= null &&getIntent().getStringExtra("id")!=null){
            id=getIntent().getStringExtra("id");
        }
        mBackbtn=findViewById(R.id.imgbtn_back);
        mBackbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        title=findViewById(R.id.practice_title);
        recyclerView=findViewById(R.id.recycleview);
        myAdapter=new MyAdapter();
        recyclerView.setAdapter(myAdapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(Practisedetai_lActivity.this));
        RequestParams requestParams=new RequestParams("http://148.70.46.9:8080/boxuegu/bxg/exercisedetail");
        requestParams.addBodyParameter("id",id);
        x.http().post(requestParams, new Callback.CacheCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Gson gson=new Gson();
                PractisedetailModer practisedetailModer=gson.fromJson(result,PractisedetailModer.class);
                    subjectList=practisedetailModer.data.subjectList;
                    myAdapter.notifyDataSetChanged();
                    title.setText(practisedetailModer.data.title);

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }

            @Override
            public boolean onCache(String result) {
                return false;
            }
        });


    }
    public class MyAdapter extends RecyclerView.Adapter<RecycleViewHolder>{

        @NonNull
        @Override
        public RecycleViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup,int i) {
            View view=View.inflate(Practisedetai_lActivity.this,R.layout.pracdetail_item_layout,null);
            RecycleViewHolder recycleViewHolder=new RecycleViewHolder(view);
            return recycleViewHolder;
        }
        @Override
        public void onBindViewHolder(@NonNull final RecycleViewHolder recycleViewHolder, final int i) {
            recycleViewHolder.mtitle.setText(" "+(i+1)+"."+subjectList.get(i).content+"。");
            recycleViewHolder.texta.setText(subjectList.get(i).optiona);
            recycleViewHolder.textb.setText(subjectList.get(i).optionb);
            recycleViewHolder.textc.setText(subjectList.get(i).optionc);
            recycleViewHolder.textd.setText(subjectList.get(i).optiond);
            recycleViewHolder.imga.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (subjectList.get(i).answer.equals("1")){
                        recycleViewHolder.imga.setBackgroundResource(R.drawable.exercises_right_icon);
                    }else {
                        recycleViewHolder.imga.setBackgroundResource(R.drawable.exercises_error_icon);
                    }if (subjectList.get(i).answer.equals("2")){
                        recycleViewHolder.imgb.setBackgroundResource(R.drawable.exercises_right_icon);
                    }if (subjectList.get(i).answer.equals("3")){
                        recycleViewHolder.imgc.setBackgroundResource(R.drawable.exercises_right_icon);
                    }if (subjectList.get(i).answer.equals("4")){
                        recycleViewHolder.imgd.setBackgroundResource(R.drawable.exercises_right_icon);
                    }
                }
            });
            recycleViewHolder.imgb.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (subjectList.get(i).answer.equals("1")){
                        recycleViewHolder.imga.setBackgroundResource(R.drawable.exercises_right_icon);
                    }if (subjectList.get(i).answer.equals("2")){
                        recycleViewHolder.imgb.setBackgroundResource(R.drawable.exercises_right_icon);
                    }else {
                        recycleViewHolder.imgb.setBackgroundResource(R.drawable.exercises_error_icon);
                    }if (subjectList.get(i).answer.equals("3")){
                        recycleViewHolder.imgc.setBackgroundResource(R.drawable.exercises_right_icon);
                    }if (subjectList.get(i).answer.equals("4")){
                        recycleViewHolder.imgd.setBackgroundResource(R.drawable.exercises_right_icon);
                    }
                }
            });
            recycleViewHolder.imgc.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (subjectList.get(i).answer.equals("1")){
                        recycleViewHolder.imga.setBackgroundResource(R.drawable.exercises_right_icon);
                    }if (subjectList.get(i).answer.equals("2")){
                        recycleViewHolder.imgb.setBackgroundResource(R.drawable.exercises_right_icon);
                    }if (subjectList.get(i).answer.equals("3")){
                        recycleViewHolder.imgc.setBackgroundResource(R.drawable.exercises_right_icon);
                    }else {
                        recycleViewHolder.imgc.setBackgroundResource(R.drawable.exercises_error_icon);
                    }if (subjectList.get(i).answer.equals("4")){
                        recycleViewHolder.imgd.setBackgroundResource(R.drawable.exercises_right_icon);
                    }
                }
            });
            recycleViewHolder.imgd.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (subjectList.get(i).answer.equals("1")){
                        recycleViewHolder.imga.setBackgroundResource(R.drawable.exercises_right_icon);
                    }if (subjectList.get(i).answer.equals("2")){
                        recycleViewHolder.imgb.setBackgroundResource(R.drawable.exercises_right_icon);
                    }if (subjectList.get(i).answer.equals("3")){
                        recycleViewHolder.imgc.setBackgroundResource(R.drawable.exercises_right_icon);
                    }if (subjectList.get(i).answer.equals("4")){
                        recycleViewHolder.imga.setBackgroundResource(R.drawable.exercises_right_icon);
                    }else {
                        recycleViewHolder.imgd.setBackgroundResource(R.drawable.exercises_error_icon);
                    }
                }
            });
        }



        @Override
        public int getItemCount() {
            return subjectList.size();
        }
    }

    public class RecycleViewHolder extends RecyclerView.ViewHolder{
        public ImageView imga,imgb,imgc,imgd;
        public TextView texta,textb,textc,textd,mtitle;

        public RecycleViewHolder(@NonNull View itemView) {
            super(itemView);
            imga=itemView.findViewById(R.id.imga);
            imgb=itemView.findViewById(R.id.imgb);
            imgc=itemView.findViewById(R.id.imgc);
            imgd=itemView.findViewById(R.id.imgd);
            texta=itemView.findViewById(R.id.texta);
            textb=itemView.findViewById(R.id.textb);
            textc=itemView.findViewById(R.id.textc);
            textd=itemView.findViewById(R.id.textd);
            mtitle=itemView.findViewById(R.id.mtitle);
        }
    }
}
